package com.sportswork.sportswork.controller.admin;

import com.sportswork.sportswork.config.SecurityUserInfo;
import com.sportswork.sportswork.core.entity.Credit;
import com.sportswork.sportswork.core.entity.Dkcj;
import com.sportswork.sportswork.core.entity.Rcpxfa;
import com.sportswork.sportswork.core.entity.User;
import com.sportswork.sportswork.core.service.dto.PageDTO;
import com.sportswork.sportswork.core.service.impl.CreditServiceImp;
import com.sportswork.sportswork.core.service.impl.DkcjServiceImp;
import com.sportswork.sportswork.core.service.impl.RcpxfaServiceImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author dengwei
 * @date 2020/2/3 12:11
 * @description
 */
@Slf4j
@Controller("DkcjController")
public class DkcjController {
    @Resource
    private DkcjServiceImp dkcjServiceImp;
    @Resource
    private CreditServiceImp creditServiceImp;

    @RequestMapping("/admin/dkcj/list")
    public String List(){
        return "/pages/view/admin/dkcj/list";
    }

    @RequestMapping("/admin/dkcj/getAllDkcj")
    @ResponseBody
    public Object getAllKtinfo(String kcmc,String jsxm,String xsxh,String xsxm){
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String xsid = "";
        String jsid = "";
        if(userDetails.getRoleId().equals("3")){
            xsid = userDetails.getUserId();
        }
        if(userDetails.getRoleId().equals("4")){
            jsid = userDetails.getUserId();
        }
        return new  PageDTO<Dkcj>().toPageDTO(dkcjServiceImp.getAllDkcjs(kcmc,jsxm,xsxh,xsxm,xsid,jsid));
    }

    @RequestMapping(value = "/admin/dkcj/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(Dkcj dkcj) {
        dkcjServiceImp.addDkcj(dkcj);
        //添加成功后需要对学分表进行更新  有则更新  无则添加 INSERT INTO dkcj (id,kcmc,kccj,xf,kcid,jsid,xsid,lrsj)
        //学生id
        String xsid = dkcj.getXsid();
        //先判断学分表中是否有改学生的数据
        Credit credit = creditServiceImp.getCreditByXsid(xsid);
        String bxkxf = "0";
        String xxkxf = "0";
        String rxkxf = "0";
        String gxkxf = "0";
        if(credit!=null){

            //查找该学生的单科成绩总和
            List<Map<String,String>> list = dkcjServiceImp.getDkcjByXsid(xsid);
            System.out.println("fanhui");
            System.out.println(list);
            for (int i = 0; i < list.size(); i++) {
                String ktlx = list.get(i).get("ktlx");
                Object ktzfs = list.get(i).get("ktzf");
                String ktzf = ktzfs.toString();
                if(ktlx.equals("必修课")){
                    bxkxf = ktzf;
                }
                if(ktlx.equals("限选课")){
                    xxkxf = ktzf;
                }
                if(ktlx.equals("任选课")){
                    rxkxf = ktzf;
                }
                if(ktlx.equals("公选课")){
                    gxkxf = ktzf;
                }
            }
            //说明存在则更新
            Credit credit1 = new Credit();
            credit1.setTitle("各类型学分情况");
            credit1.setXxkxf(xxkxf);
            credit1.setBxkxf(bxkxf);
            credit1.setRxkxf(rxkxf);
            credit1.setGxkxf(gxkxf);
            credit1.setXsid(xsid);
            creditServiceImp.setCreditByXsid(credit1);
        }else{
            //不存在 添加新数据
            String ktlx = dkcj.getKtlx();
            String xf = dkcj.getXf();
            if(ktlx.equals("必修课")){
                bxkxf = xf;
            }
            if(ktlx.equals("限选课")){
                xxkxf = xf;
            }
            if(ktlx.equals("任选课")){
                rxkxf = xf;
            }
            if(ktlx.equals("公选课")){
                gxkxf = xf;
            }
            //INSERT INTO credit (id, title,bxkxf,xxkxf,rxkxf,gxkxf,name,xsid,djrq,lrsj)
            Credit credit1 = new Credit();
            credit1.setTitle("各类型学分情况");
            credit1.setXxkxf(xxkxf);
            credit1.setBxkxf(bxkxf);
            credit1.setRxkxf(rxkxf);
            credit1.setGxkxf(gxkxf);
            credit1.setName("");
            credit1.setXsid(xsid);
            credit1.setDjrq("");
            creditServiceImp.addCredit(credit1);
        }
        return "success";
    }

    @RequestMapping(value = "/admin/dkcj/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(Dkcj dkcj) {
        dkcjServiceImp.setDkcj(dkcj);
        String xsid = dkcj.getXsid();

        String bxkxf = "0";
        String xxkxf = "0";
        String rxkxf = "0";
        String gxkxf = "0";
        //查找该学生的单科成绩总和
        List<Map<String,String>> list = dkcjServiceImp.getDkcjByXsid(xsid);
        System.out.println("fanhui");
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            String ktlx = list.get(i).get("ktlx");
            Object ktzfs = list.get(i).get("ktzf");
            String ktzf = ktzfs.toString();
            if(ktlx.equals("必修课")){
                bxkxf = ktzf;
            }
            if(ktlx.equals("限选课")){
                xxkxf = ktzf;
            }
            if(ktlx.equals("任选课")){
                rxkxf = ktzf;
            }
            if(ktlx.equals("公选课")){
                gxkxf = ktzf;
            }
        }
        //说明存在则更新
        Credit credit1 = new Credit();
        credit1.setTitle("各类型学分情况");
        credit1.setXxkxf(xxkxf);
        credit1.setBxkxf(bxkxf);
        credit1.setRxkxf(rxkxf);
        credit1.setGxkxf(gxkxf);
        credit1.setXsid(xsid);
        creditServiceImp.setCreditByXsid(credit1);


        return "success";
    }

    @RequestMapping(value = "/admin/dkcj/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete( String id ,String xsid) {
        dkcjServiceImp.deleteDkcj(id);
        String bxkxf = "0";
        String xxkxf = "0";
        String rxkxf = "0";
        String gxkxf = "0";
        //查找该学生的单科成绩总和
        List<Map<String,String>> list = dkcjServiceImp.getDkcjByXsid(xsid);
        System.out.println("fanhui");
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            String ktlx = list.get(i).get("ktlx");
            Object ktzfs = list.get(i).get("ktzf");
            String ktzf = ktzfs.toString();
            if(ktlx.equals("必修课")){
                bxkxf = ktzf;
            }
            if(ktlx.equals("限选课")){
                xxkxf = ktzf;
            }
            if(ktlx.equals("任选课")){
                rxkxf = ktzf;
            }
            if(ktlx.equals("公选课")){
                gxkxf = ktzf;
            }
        }
        //说明存在则更新
        Credit credit1 = new Credit();
        credit1.setTitle("各类型学分情况");
        credit1.setXxkxf(xxkxf);
        credit1.setBxkxf(bxkxf);
        credit1.setRxkxf(rxkxf);
        credit1.setGxkxf(gxkxf);
        credit1.setXsid(xsid);
        creditServiceImp.setCreditByXsid(credit1);


        return "true";
    }

    @RequestMapping(value = "/admin/dkcj/deletes", method = RequestMethod.POST)
    @ResponseBody
    public String deletes(@RequestParam String ids) {
        dkcjServiceImp.deleteDkcjs(ids );
        return "true";
    }

    @RequestMapping(value = "/admin/dkcj/getUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public SecurityUserInfo getUserInfo(String oldpsw , String newpsw) {
        SecurityUserInfo userDetails = (SecurityUserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        return userDetails;
    }





}
